package com.creditcmsplt.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.creditcmsplt.entity.CreditCsv;

/**
 * 数据库操作DAO：creditauoth表 厂商授权表
 * 
 * @author 雷阳军
 *
 */
public interface CreditCsvMapper {

	@Insert("INSERT INTO t_credit_csv(csvconvertid, filename,ffilename,downloadcode,deskey,despwd,identify,status,created_at,updated_at) VALUES "
			+ "(#{csvconvertid,jdbcType=INTEGER}, #{filename,jdbcType=VARCHAR}, #{ffilename,jdbcType=VARCHAR}, #{downloadcode,jdbcType=VARCHAR}, #{deskey,jdbcType=VARCHAR},#{despwd,jdbcType=VARCHAR},#{identify,jdbcType=VARCHAR},#{status,jdbcType=VARCHAR},NOW(),NOW())")
	public void insert(CreditCsv CreditCsv);
	
	@Update("UPDATE t_credit_csv set status=#{status},ffilename=#{ffilename},updated_at=NOW() where filename=#{filename} ")
	public void update(@Param("filename") String pdfname,@Param("ffilename") String ffilename,@Param("status") Integer status);
	
	@Select("select id,csvconvertid,filename,downloadcode, deskey , despwd,identify,status from t_credit_csv where downloadcode=#{downloadcode} and status=0 LIMIT 1")
	public CreditCsv checkDlownloadFlag(@Param("downloadcode")String downloadcode);
	
	@Select("select id,csvconvertid,filename,downloadcode, deskey , despwd,identify,status from t_credit_csv where downloadcode in ( select downloadcode from t_credit_csv where filename=#{filename}) and status=0 LIMIT 1")
	public CreditCsv checkZipFlag(@Param("filename")String filename);
			
	@Select("select id,csvconvertid,ffilename,filename,downloadcode, deskey , despwd,identify,status from t_credit_csv where downloadcode in ( select downloadcode from t_credit_csv where filename=#{filename}) and status=1")
	public List<CreditCsv> getZipFile(@Param("filename")String filename);
	
	@Select("select id,csvconvertid,filename,downloadcode, deskey , despwd,identify,status from t_credit_csv where downloadcode=#{downloadcode} limit 1")
	public CreditCsv getCreditCsvByDownloadCode(@Param("downloadcode")String downloadcode);
	
	/**
	 * 获取可以下载的文件个数
	 * @param downloadcode
	 * @return
	 */
	@Select("select CONVERT(status,SIGNED) status,CONVERT(count(1),SIGNED) statuscount from t_credit_csv where downloadcode=#{downloadcode} group by status")
	public List<Map<String,Object>> getDownloadSizeInfoByDownloadcode(@Param("downloadcode")String downloadcode);
}
